package com.dinghui.salary.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dinghui.salary.mapper.SalaryChangeApplyMapper;
import com.dinghui.salary.pojo.Page;
import com.dinghui.salary.pojo.SalaryChangeApply;
import com.dinghui.salary.pojo.SalaryType;
import com.dinghui.salary.service.SalaryChangeApplyService;
import com.dinghui.salary.utils.PagedGridResult;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author tanrentao
 * @date 2021-09-06
 * @desc
 */
@Slf4j
@Service(version = "2.0.0")
public class SalaryChangeApplyServiceImpl extends BaseService implements SalaryChangeApplyService {

    @Autowired
    private SalaryChangeApplyMapper salaryChangeApplyMapper;

    @Override
    public int add(SalaryChangeApply salaryChange) {
        int i = salaryChangeApplyMapper.insertSelective(salaryChange);
        return i;
    }
    public int modify(SalaryChangeApply salaryChange) {
        Example example = new Example(SalaryChangeApply.class);
        example.createCriteria().andEqualTo("id",salaryChange.getId());
        int i = salaryChangeApplyMapper.updateByExampleSelective(salaryChange,example);
        return i;
    }


    @Override
    public Object Delete(SalaryChangeApply dto) {
        Example example = new Example(SalaryChangeApply.class);
        example.createCriteria().andEqualTo("id",dto.getId());
        int i = salaryChangeApplyMapper.updateByExampleSelective(dto,example);
        return i;
    }

    @Override
    public PagedGridResult FindAll(Page dto) {

        Example example = new Example(SalaryChangeApply.class);
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(dto.getName())) {
            criteria.andLike("name", "%" + dto.getName() + "%");
        }
        if (StringUtils.isNotBlank(dto.getYear_month())) {
            criteria.andEqualTo("yearMonths", dto.getYear_month() );
        }
        if (StringUtils.isNotBlank(dto.getId_number())) {
            criteria.andLike("idNumber", "%" + dto.getId_number() + "%");
        }
        if (StringUtils.isNotBlank(dto.getReason())) {
            criteria.andLike("reason", "%" + dto.getReason() + "%");
        }
        if (StringUtils.isNotBlank(dto.getState())) {
            criteria.andLike("state", "%" + dto.getState() + "%");
        }
        criteria.andEqualTo("isDelete",  0);
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<SalaryChangeApply> list = salaryChangeApplyMapper.selectByExample(example);

        return setterPagedGrid(list, dto.getPage());
    }
}
